package cn.etynit.service.impl;

import cn.etynit.domain.ExpenseCar;
import cn.etynit.exception.GlobalException;
import cn.etynit.exception.GlobalExceptionCodeEnum;
import cn.etynit.mapper.ExpenseCarMapper;
import cn.etynit.result.JsonResult;
import cn.etynit.service.IExpenseCarService;
import cn.etynit.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.assertj.core.internal.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author etyn
 * @since 2022-12-08
 */
@Service
public class ExpenseCarServiceImpl extends ServiceImpl<ExpenseCarMapper, ExpenseCar> implements IExpenseCarService {
    @Autowired
    private ExpenseCarMapper expenseCarMapper;

    /**
     * 查询剩余停车位
     * @param type
     * @return
     */
    @Override
    public Integer surplusParkingSpace(Integer type) {
        // 查询所有办理停车位条数
        Integer i = expenseCarMapper.surplusParkingSpace(type);
        // 获取初始车位100
        Integer totalParkingSpaces = ExpenseCar.TOTAL_PARKING_SPACES;
        if (totalParkingSpaces > i){
            return totalParkingSpaces - i;
        } else {
            throw new GlobalException(GlobalExceptionCodeEnum.ERROR); // 小车车位不够报错
        }
    }

    /**
     * 根据业主id查询是否已经办理过停车位了
     * @param id
     * @return
     */
    @Override
    public void selectByUserId(Long id) {
        Wrapper<ExpenseCar> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",id);
        ExpenseCar expenseCar = selectOne(wrapper);
        AssertUtil.isNull(expenseCar,GlobalExceptionCodeEnum.ERROR); // 办理过停车位了报错
    }
}
